﻿using System;
using System.Collections;
using System.Threading;

using GameServer.Networking.Packets;
using GameServer.Virtual_Objects.Room;

namespace GameServer.Managers
{
    class RoomManager
    {
        private static Hashtable[] _Rooms = new Hashtable[5];
        private static Thread _roomTick;
        private static Thread _roomRefresh;

        ~RoomManager()
        {
          
        }

        public static void setup()
        {
            for (int I = 1; I < 5; I++)
            {
                _Rooms[I] = new Hashtable();
            }
            _roomTick = new Thread(new ThreadStart(roomTick));
            _roomTick.Priority = ThreadPriority.AboveNormal;
            _roomTick.Start();
        }

        public static ArrayList getRoomsInChannel(int ID)
        {
            try
            {
                return new ArrayList(_Rooms[ID]);
            }
            catch { return null; }
        }

        public static int getOpenID(int Channel)
        {
            try
            {
                for (int I = 0; I < 100; I++)
                {
                    if (_Rooms[Channel].ContainsKey(I) == false)
                    {
                        return I;
                    }
                }
                return -1;
            }
            catch { return -1; }
        }

        public static virtualRoom getRoom(int Channel, int ID)
        {
            try
            {
                if (_Rooms[Channel].ContainsKey(ID))
                {
                    return (virtualRoom)_Rooms[Channel][ID];
                }
                return null;
            }
            catch { return null; }
        }

        public static ArrayList getRoomsInChannel(int Channel, int Page)
        {
            try
            {
                ArrayList xRooms = new ArrayList();

                for (int I = (14 * Page); I < (14 * (Page + 1)); I++)
                {
                    if (_Rooms[Channel].ContainsKey(I))
                        xRooms.Add(_Rooms[Channel][I]);
                }
                return xRooms;
            }
            catch { return null; }
        }

        public static bool addRoom(int Channel, int ID, virtualRoom Room)
        {
            try
            {
                if (_Rooms[Channel].ContainsKey(ID) == false)
                {
                    Log.WriteLine("Added Room [" + ID + ":" + Room.Name + "] to the Room List.");
                    _Rooms[Channel].Add(ID, Room);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
                return false;
            }
        }

        public static bool removeRoom(int Channel, int ID)
        {
            try
            {
                if (_Rooms[Channel].ContainsKey(ID))
                {
                    Log.WriteLine("Removed Room [" + ID + "] from the Room List.");
                    _Rooms[Channel].Remove(ID);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
                return false;
            }
        }

        public static void shutDown()
        {
            for (int I = 1; I < 5; I++)
            {
                foreach (virtualRoom Room in ((Hashtable)_Rooms[I].Clone()).Values)
                {
                    /* Update Room */
                    Room.remove();
                }
            }
        }

        private static void roomTick()
        {
            try
            {
                while (true)
                {
                    for (int I = 1; I < 5; I++)
                    {
                        foreach (virtualRoom Room in ((Hashtable)_Rooms[I].Clone()).Values)
                        {
                            if (Room.PlayerCount <= 0)
                                Room.remove();
                            lock (Room)
                            {
                                Room.update(); /* Update Room */
                            }
                        }
                    }
                    Thread.Sleep(200);
                }
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
            }
        }
    }
}
